package com.zhuangfei.hpuhelper.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hpu.bean.Score;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.ScoreParser;
import com.hpu.service.LoginService;
import com.hpu.service.ScoreService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.generalpage.BaseFragment;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.view.MyApplication;
import com.zhuangfei.smartalert.core.LoadAlert;
import com.zhuangfei.smartalert.core.MessageAlert;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import cn.lzf.serviceplatform.R;

public class ScoreFragment extends LazyLoadFragment {

	/**
	 * 复用 view
	 */
	private View mView;
	private Context context;

	/**
	 * 列表相关
	 */
	List<Map<String, String>> items = new ArrayList<>();
	SimpleAdapter adapter;

	/**
	 * 下拉刷新
	 */
	private ListView mListView;

	/**
	 * 业务类
	 */
	private ScoreService scoreService;
	private LoginService loginService;
	private MessageAlert messageAlert;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mView=inflater.inflate(R.layout.fragment_score,container,false);
		return mView;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		initView();
		initEvent();
		super.onViewCreated(view,savedInstanceState);
	}

	@Override
	protected void lazyLoad() {
		scoreService.getScore();
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		context = getActivity();
		messageAlert=new MessageAlert(getActivity()).create();

		// 业务逻辑对象
		scoreService = new ScoreService(handler, getActivity());
		loginService = new LoginService(getActivity(),handler);

		mListView = (ListView) mView.findViewById(R.id.id_score_listview);

		// 适配器
		adapter = new SimpleAdapter(getActivity(), items, R.layout.item_score_layout,
				new String[] { "subject","value" }, new int[] { R.id.id_score_name,
						R.id.id_score_value });
		mListView.setAdapter(adapter);
	}
	
	private void initEvent() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				Map map=items.get(arg2);
				String content="课程名："+map.get("subject")+"\n课程号："+map.get("number")+"\n课序号："+
				map.get("order")+"\n学分："+map.get("credit")+"\n成绩："+map.get("value")+"\n排名："+map.get("valueOrder");
				messageAlert.setTitle("成绩详情")
				.setContent(content)
				.show();
			}
			
		});
	}
	

	/**
	 * 显示课程
	 */
	protected void showScore(List<Score> scoreList) {
		if(scoreList==null) return;

		items.clear();
		if(scoreList.size()==0){
			getViewHelper().showEmptyView("未查到期末成绩");
		}
		for (Score score : scoreList) {
			Map<String, String> map = new HashMap<>();
			map.put("subject", score.getSubject());
			map.put("value", score.getValue());
			map.put("number", score.getNumber());
			map.put("order", score.getOrder());
			map.put("credit", score.getCredit());
			map.put("valueOrder", score.getValueOrder());
			items.add(map);
		}
		adapter.notifyDataSetChanged();
	}
	
	DefaultHandler handler = new DefaultHandler(MyApplication.getInstance()) {
		public void handleMessage(Message msg) {
			getViewHelper().showSuccessView();
			super.handleMessage(msg);
			
			switch (msg.what) {
			case HttpConstants.QUERY_SCORE_SUCCESS:
				// 成绩解析器将返回结果解析为集合
				ScoreParser scoreParser = new ScoreParser();
				List<Score> scoreList = scoreParser.parse(msg.obj.toString());
				showScore(scoreList);
				break;

			// VPN过期
			case HttpConstants.VPN_TOOLONG:
				if(handler.isIgnoreVpnExpired()){
					break;
				}
				getViewHelper().showLoadingView();
				loginService.login(ShareTools.get(getActivity(), ShareConstants.STORE_USER, "").toString(),
						ShareTools.get(getActivity(), ShareConstants.STORE_PASSWORD1, "").toString(),
						ShareTools.get(getActivity(), ShareConstants.STORE_PASSWORD2, "").toString());
				break;

			// 登录成功
			case HttpConstants.LOGIN_SUCCESS:
				getViewHelper().showLoadingView();
				ShareTools.put(getActivity(), ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
				scoreService.getScore();
				break;

			}
		};
	};

}
